package com.esun.service.service.impl;

import com.alibaba.excel.EasyExcel;
import com.esun.api.request.ExampleRequestDto;
import com.esun.api.request.SortRequestDto;
import com.esun.api.response.ExampleResponseDto;
import com.esun.common.constant.SystemsConstant;
import com.esun.common.enums.Message;
import com.esun.common.utils.FileUtils;
import com.esun.common.utils.GuidUtils;
import com.esun.common.utils.MessageUtils;
import com.esun.dao.das.ExampleDas;
import com.esun.dao.eo.ExampleEo;
import com.esun.dao.eo.PageEo;
import com.esun.dao.eo.SortEo;
import com.esun.service.service.ExampleService;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 样例表(Example)表服务实现类
 *
 * @author John.xiao
 * @since 2021-08-17 15:36:40
 */
@Service
public class ExampleServiceImpl implements ExampleService {
	@Resource
	private ExampleDas exampleDas;

	@Value("${file.path:/}")
	String filePath;

	@Override
	public PageInfo<ExampleResponseDto> getExampleByPage(ExampleRequestDto requestDto) {
		List<ExampleResponseDto> responseDtoList = new ArrayList<>();
		PageEo pageEo = PageEo.builder().pageIndex(requestDto.getPageIndex()).pageSize(requestDto.getPageSize()).build();
		ExampleEo exampleEo = ExampleEo.builder().build();
		BeanUtils.copyProperties(requestDto,exampleEo);
		List<SortEo> sortEoList = new ArrayList<>();
		List<SortRequestDto> sortRequestDtoList = requestDto.getSortList();
		if (CollectionUtils.isNotEmpty(sortRequestDtoList)){
			sortRequestDtoList.forEach((sortRequestDto -> {
				SortEo sortEo = new SortEo();
				BeanUtils.copyProperties(sortRequestDto,sortEo);
				sortEoList.add(sortEo);
			}));
		}
		List<ExampleEo> eoList = exampleDas.query(exampleEo,sortEoList,pageEo);
		for (ExampleEo eo : eoList){
			ExampleResponseDto responseDto = new ExampleResponseDto();
			BeanUtils.copyProperties(eo,responseDto);
			responseDtoList.add(responseDto);
		}
		PageInfo<ExampleResponseDto> responseDtoPageInfo = new PageInfo<>(responseDtoList);
		return responseDtoPageInfo;
	}

	@Override
	public List<ExampleResponseDto> addExample(List<ExampleRequestDto> requestDtoList) {
		List<ExampleEo> eoList = new ArrayList<>();
		List<ExampleResponseDto> responseDtoList = new ArrayList<>();
		for (ExampleRequestDto requestDto : requestDtoList){
			ExampleEo exampleEo = ExampleEo.builder().build();
			BeanUtils.copyProperties(requestDto,exampleEo);
			ExampleResponseDto responseDto = new ExampleResponseDto();
			BeanUtils.copyProperties(exampleEo,responseDto);
			String code = Message.QUERY_SUCCESS.getCode();
			responseDto.setErrorCode(code);
			responseDto.setMsg(exampleEo.getName()+MessageUtils.getInfoByCode(code));
			responseDtoList.add(responseDto);
			eoList.add(exampleEo);
		}
		for (ExampleResponseDto responseDto : responseDtoList) {
			if (!Message.QUERY_SUCCESS.getCode().equals(responseDto.getErrorCode())) {
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
				return responseDtoList;
			}
		}
		if (CollectionUtils.isNotEmpty(eoList)){
			exampleDas.insertBatch(eoList);
		}
		return responseDtoList;
	}

	@Override
	public List<ExampleResponseDto> updateExample(List<ExampleRequestDto> requestDtoList) {
		List<ExampleEo> upset = new ArrayList<>();
		List<ExampleEo> querySet = new ArrayList<>();
		List<ExampleResponseDto> responseDtoList = new ArrayList<>();
		for (ExampleRequestDto requestDto : requestDtoList){
			ExampleEo updateEo = ExampleEo.builder().build();
			ExampleEo queryEo = ExampleEo.builder().build();
			BeanUtils.copyProperties(requestDto,updateEo);
			queryEo.setName(requestDto.getName());
			ExampleResponseDto responseDto = new ExampleResponseDto();
			BeanUtils.copyProperties(updateEo,responseDto);
			String code = Message.QUERY_SUCCESS.getCode();
			responseDto.setErrorCode(code);
			responseDto.setMsg(updateEo.getName() + MessageUtils.getInfoByCode(code));
			responseDtoList.add(responseDto);
			upset.add(updateEo);
			querySet.add(queryEo);
		}
		for (ExampleResponseDto responseDto : responseDtoList) {
			if (!Message.QUERY_SUCCESS.getCode().equals(responseDto.getErrorCode())) {
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
				return responseDtoList;
			}
		}
		for (ExampleEo eo: upset) {
		}
		return responseDtoList;
	}

	@Override
	public List<ExampleResponseDto> deleteExample(List<ExampleRequestDto> requestDtoList) {
		List<ExampleEo> eoList = new ArrayList<>();
		List<ExampleResponseDto> responseDtoList = new ArrayList<>();
		for (ExampleRequestDto requestDto : requestDtoList){
			ExampleEo exampleEo = ExampleEo.builder().build();
			BeanUtils.copyProperties(requestDto,exampleEo);
			ExampleResponseDto responseDto = new ExampleResponseDto();
			BeanUtils.copyProperties(exampleEo,responseDto);
			String code = Message.QUERY_SUCCESS.getCode();
			responseDto.setErrorCode(code);
			responseDto.setMsg(exampleEo.getName() + MessageUtils.getInfoByCode(code));
			responseDtoList.add(responseDto);
			eoList.add(exampleEo);
		}
		for (ExampleResponseDto responseDto : responseDtoList) {
			if (!Message.QUERY_SUCCESS.getCode().equals(responseDto.getErrorCode())) {
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
				return responseDtoList;
			}
		}
		if(CollectionUtils.isNotEmpty(eoList)){
			eoList.forEach(exampleEo -> exampleDas.delete(exampleEo) );
		}
		return responseDtoList;
	}

	@Override
	public List<ExampleResponseDto> importExample(List<ExampleEo> eoList) {
		List<ExampleResponseDto> exampleResponseDtoList = Lists.newArrayList();
		for (ExampleEo eo : eoList){
			ExampleResponseDto responseDto = new ExampleResponseDto();
			BeanUtils.copyProperties(eo,responseDto);
			exampleResponseDtoList.add(responseDto);
		}
		return exampleResponseDtoList;
	}

	@Override
	public void exportExample(ExampleRequestDto requestDto) {
		ExampleEo exampleEo = ExampleEo.builder().build();
		BeanUtils.copyProperties(requestDto,exampleEo);
		List<ExampleEo> eoList = exampleDas.query(exampleEo, Collections.emptyList(), PageEo.builder().build());
		String programUrl = System.getProperty("user.dir");
		String fileName = programUrl.concat(filePath).concat(GuidUtils.getGuid()).concat(SystemsConstant.HEADQUARTERS);
		EasyExcel.write(fileName, ExampleEo.class).sheet("用户信息导出文件").doWrite(eoList);
		FileUtils fileUtils = new FileUtils();
		fileUtils.downLoad(fileName);
	}

	@Override
	public void downloadTemplate(String path) {
		String programUrl = System.getProperty("user.dir");
		path = programUrl + path;
		FileUtils fileUtils = new FileUtils();
		fileUtils.downLoad(path);
	}
}